package cn.gailvlun.gll.presentation.login;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityPhoneBindingBinding;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.login.bean.CountryCode;
import cn.gailvlun.gll.presentation.login.viewmodel.PhoneBindingViewModel;
import cn.gailvlun.gll.util.PhoneUtil;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.util.SPUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import cn.gailvlun.gll.widget.edittext.FilterType;



public class PhoneBindingActivity extends BaseActivity {

    private ActivityPhoneBindingBinding mBinding;

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, PhoneBindingActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_phone_binding);
        PhoneBindingViewModel viewModel = new PhoneBindingViewModel(mContext, mBinding);
        mBinding.setVm(viewModel);
        CountryCode countryCode = SPUtil.get(SPUtil.GLOBAL, "countryCode", CountryCode.class);
        if (countryCode == null) {
            viewModel.setCountryNameAndCode("CN +" + PhoneUtil.getCountryCodeByDefault());
        } else {
            int indexStart = countryCode.getCountryName().indexOf("(");
            int indexEnd = countryCode.getCountryName().indexOf(")");
            String text = countryCode.getCountryName().substring(indexStart + 1, indexEnd).trim() + " " + countryCode.getCountryCode();
            viewModel.setCountryNameAndCode(text);
        }
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.cetPhone.startFilter(FilterType.PHONE);
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ProfileUtil.logOut();
        LoginActivity.openActivity(mContext, true);
    }


}
